﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
using System.Web.SessionState;
using System.Text.RegularExpressions;

namespace WMS.Handler
{
    public class DeptHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            DeptBll deptbll = new DeptBll();
            EventLogBll eventlogbll = new EventLogBll();
            string action = string.Empty;
            string dept_id = string.Empty;
            string dept_name = string.Empty;

            #region 处理参数
            action = context.Request.Form["action"];
            if (action == null || action == "")
            {
                action = context.Request.QueryString["action"];
            }
            dept_id = context.Request.Form["txtDeptid"];
            if (string.IsNullOrEmpty(dept_id))
            {
                dept_id = context.Request.QueryString["deptid"];
            }
            dept_name = context.Request.Form["txtDeptName"];
            #endregion
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("dept_id", dept_id);
            dic.Add("dept_name", dept_name);

            string masterid = string.Empty;//当前登录用户
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                masterid = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
            }
            switch (action)
            {
                case "add":
                    bool judgeint = JudgeIntOrNot(dept_id);
                    if (judgeint == true)
                    {
                        bool resultadd = deptbll.InsertDeptData(dic);
                        if (resultadd == true)
                        {
                            //成功
                            context.Response.Write(success("SystemManagement/DeptManagement.aspx"));
                            Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                            eventlogdic.Add("masterid", masterid);
                            eventlogdic.Add("msg", "新增部门：" + dept_id + "");
                            eventlogbll.InsertEventLog(eventlogdic);
                        }
                        else
                        {
                            //失败
                            context.Response.Write(failure("修改失败"));
                        }
                    }
                    else
                    {
                        //失败
                        context.Response.Write(failure("部门编号只能是数字!"));
                    }
                    break;
                case "edit":
                    bool resultedit = deptbll.UpdateDeptData(dic);
                    if (resultedit == true)
                    {
                        //成功
                        context.Response.Write(success("SystemManagement/DeptManagement.aspx"));
                        Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                        eventlogdic.Add("masterid", "");
                        eventlogdic.Add("msg", "修改部门：" + dept_id + "");
                        eventlogbll.InsertEventLog(eventlogdic);
                    }
                    else
                    {
                        //失败
                        context.Response.Write(failure("修改失败"));
                    }
                    break;
                case "delete":
                    bool resultdelete = deptbll.DeleteDeptData(dept_id);
                    if (resultdelete == true)
                    {
                        //成功
                        context.Response.Write(success1());
                        Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                        eventlogdic.Add("masterid", "");
                        eventlogdic.Add("msg", "删除部门：" + dept_id + "");
                        eventlogbll.InsertEventLog(eventlogdic);
                    }
                    else
                    {
                        //失败
                        context.Response.Write(failure());
                    }
                    break;
            }
        }

        /// <summary>
        /// 判断部门编号是否是数字
        /// </summary>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public bool JudgeIntOrNot(string dept_id)
        {
            bool judge = false;
            try
            {
                judge = Regex.IsMatch(dept_id.ToString().Trim(), @"^\d+$", RegexOptions.IgnoreCase);
            }
            catch
            {
                judge = false;
            }
            return judge;
        }


        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        bool IHttpHandler.IsReusable
        {
            get { throw new NotImplementedException(); }
        }
    }
}